package com.hst.capacity.application.biz.impl;

import com.hst.capacity.application.biz.BrandBiz;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.auth.TokenEntity;
import com.hst.capacity.domain.model.entity.brand.BrandUserEntity;
import com.hst.capacity.domain.model.request.SignInParam;
import com.hst.capacity.domain.model.request.brand.BrandUserParam;
import com.hst.capacity.domain.model.request.brand.BrandUserSearchParam;
import com.hst.capacity.domain.model.response.PagingResultVO;
import com.hst.capacity.domain.model.response.auth.LoginUser;
import com.hst.capacity.domain.model.response.auth.TokenVO;
import com.hst.capacity.domain.model.response.brand.BrandUserVO;
import com.hst.capacity.domain.service.BrandService;
import com.hst.capacity.domain.service.TokenService;
import com.hst.capacity.infra.convertor.AuthConvertor;
import com.hst.capacity.infra.convertor.BrandConvertor;
import com.hst.capacity.infra.persistence.jpa.brand.BrandRepository;
import com.hst.capacity.infra.persistence.jpa.brand.BrandUserRepository;
import com.hst.capacity.infra.persistence.mybatis.BrandMapper;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class BrandBizImpl implements BrandBiz {

    @Autowired
    private BrandRepository brandRepository;

    @Autowired
    private BrandUserRepository brandUserRepository;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private BrandService brandService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public TokenVO signIn(SignInParam param) {
        // 1 按账号、密码身份验证
        LoginUser user = brandService.authenticate(param.getUsername(), param.getPassword());

        // 2 验证通过后创建Token并返回
        return createToken(user.getUser().getId());
    }

//    @Override
//    public UserBindingVO getUserBindingInfo(String userId) {
//
//        // 1 查询运力商用户实体
//        BrandUserEntity brandUserEntity = brandUserRepository.findByIdBrandUser(userId);
//
//        // 2 查询运力商实体
//        BrandEntity brandEntity = brandRepository.findByIdBrand(brandUserEntity.getIdBrand());
//
//        //3 查找城市名
//        SysRegionEntity byCode = regionRepository.findByCode(franchiseeEntity.getRegionCode());
//
//        UserBindingVO bindingVO = AuthConvertor.INSTANCE.convert(brandUserEntity, franchiseeEntity, byCode);
//
//        //4 查找用户角色
//        List<String> roles = userRoleRepository.findByUserId(userId);
//        bindingVO.setRoles(roles);
//
//        return bindingVO;
//    }

    @Override
    public String logout(String userId) {
        tokenService.destroyAccessToken(userId);
        return "OK";
    }

    @Override
    public PagingResultVO<BrandUserVO> search(BrandUserSearchParam param) {
        PagingResultVO<BrandUserVO> result = new PagingResultVO<>(brandMapper.searchTotal(param), brandMapper.search(param));
        return result;
    }

    @Override
    public String save(BrandUserParam param) {
        BrandUserEntity entity;
        String idBrandUser = param.getIdBrandUser();
        if (StringUtil.isNotBlank(idBrandUser)) {
            entity = brandUserRepository.findByIdBrandUser(idBrandUser);
            if (entity == null)
                throw new BusinessException(5002, "未找到对应的用户数据。");
            idBrandUser = entity.getIdBrandUser();
        }

        entity = BrandConvertor.INSTANCE.convert(param);
        if (StringUtil.isNotBlank(idBrandUser)) {
            entity.setIdBrandUser(idBrandUser);
            if (StringUtil.isNotBlank(param.getPassword()))
            {
                entity.setPassword(passwordEncoder.encode(param.getPassword()));
            }
        } else {
            entity.setErrorumber(0);
            if (StringUtil.isNotBlank(param.getPassword()))
            {
                entity.setPassword(passwordEncoder.encode(param.getPassword()));
            }
        }
        return brandUserRepository.save(entity).getIdBrandUser();
    }

    private TokenVO createToken(String userId) {
        // 创建访问令牌
        TokenEntity tokenEntity = tokenService.createAccessToken(userId);
        return AuthConvertor.INSTANCE.convert(tokenEntity);
    }

}
